{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "473a3020",
   "metadata": {},
   "source": [
    "# 宿舍同学信息存放"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "id": "8fe87ef4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 146,
   "id": "07ba233f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 113,\n",
       " 'error_message': 'FACESET_EXIST',\n",
       " 'request_id': '1648728283,23112936-6cae-4ada-b794-900f25af5ed8'}"
      ]
     },
     "execution_count": 146,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 登录\n",
    "API_key = 'Ks5_WYWbPV5hMgN9kmGZquIMqmVKOYax'\n",
    "API_sercret = 'Fn3E71FHFW0B7lzi_7HzbbKLDaXGqbZV'\n",
    "# 创建人脸集合\n",
    "create_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/create\"\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'display_name': '西9',\n",
    "    'outer_id': '4人',\n",
    "}\n",
    "r = requests.post(create_url, params=payload)\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "id": "f9ad4b66",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 89,\n",
       " 'facesets': [{'faceset_token': '8373ca2a5977cd182279fd2c53d07cb8',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': '1b0a9ce2cbf2ba4760b6aa45ed4f83e1',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': '8a625079168e4c2aa41d5d234f67c2ed',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': 'a6b4612faee106e667422facf6366b1c',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': '564228df697a560855775543e0df44a6',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': '9c33425c8cfdcf5c1a3c507a18f4c517',\n",
       "   'outer_id': 'API020305',\n",
       "   'display_name': '西9宿舍',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': '82530f1cec317fba9e58bc5e9abb4dbc',\n",
       "   'outer_id': '4人',\n",
       "   'display_name': '西9宿舍',\n",
       "   'tags': ''}],\n",
       " 'request_id': '1648725793,bf2cae5c-2971-47b4-97ab-fa62e95fe688'}"
      ]
     },
     "execution_count": 110,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取人脸库列表及信息\n",
    "getFaces_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/getfacesets\"\n",
    "\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "}\n",
    "\n",
    "r = requests.post(getFaces_url, payload)\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "id": "b29b4039",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'82530f1cec317fba9e58bc5e9abb4dbc'"
      ]
     },
     "execution_count": 111,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取 faceset_token\n",
    "faceset_token = r.json()[\"facesets\"][-1][\"faceset_token\"]\n",
    "faceset_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "74d12b14",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 147,
   "id": "15790054",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 更新人脸数据库\n",
    "update_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/update\"\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'faceset_token':faceset_token,\n",
    "    'user_data':'203',\n",
    "}\n",
    "r = requests.post(url = update_FaceSet_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 148,
   "id": "3d6e287d",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '82530f1cec317fba9e58bc5e9abb4dbc',\n",
       " 'request_id': '1648728387,2af972b5-530d-4078-a4ec-79496cc32e36',\n",
       " 'time_used': 76,\n",
       " 'outer_id': '4人'}"
      ]
     },
     "execution_count": 148,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 149,
   "id": "e4c6f634",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 检查数据集合\n",
    "getDetail_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail\"\n",
    "\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    \"faceset_token\": faceset_token,\n",
    "}\n",
    "\n",
    "r = requests.post(getDetail_url, payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 150,
   "id": "64bcfcfa",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '82530f1cec317fba9e58bc5e9abb4dbc',\n",
       " 'tags': '',\n",
       " 'time_used': 93,\n",
       " 'user_data': '203',\n",
       " 'display_name': '西9宿舍',\n",
       " 'face_tokens': [],\n",
       " 'face_count': 0,\n",
       " 'request_id': '1648728429,2bae1f2b-1258-4a36-9a76-c65a7df2439a',\n",
       " 'outer_id': '4人'}"
      ]
     },
     "execution_count": 150,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "id": "0974c5cb",
   "metadata": {},
   "outputs": [],
   "source": [
    "import detect,addface"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "id": "f09f5a96",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'6e65d0061f2499a1681a3228710daab3'"
      ]
     },
     "execution_count": 133,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 人脸添加\n",
    "first = detect.face_detect(API_key,API_sercret,\"weiting.jpeg\")\n",
    "face01_token = first[\"faces\"][0][\"face_token\"]\n",
    "face01_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 141,
   "id": "17ba562b",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "face_addface() takes 3 positional arguments but 4 were given",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-141-65911de69d85>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0maddface\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mface_addface\u001b[0m\u001b[1;33m(\u001b[0m \u001b[0mAPI_key\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mAPI_sercret\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfaceset_token\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mface01_token\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      2\u001b[0m \u001b[0maddface\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0maddFace\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mapi_key\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mapi_secret\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mfaceset_token\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mface01_token\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mTypeError\u001b[0m: face_addface() takes 3 positional arguments but 4 were given"
     ]
    }
   ],
   "source": [
    "addface.face_addface( API_key,API_sercret, faceset_token, face01_token)\n",
    "addface.addFace(api_key, api_secret,faceset_token, face01_token)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "00b5ad8a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
